package com.wisemoresystem.seal.action.user;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.ne.framework.data.util.ChkTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wisemoresystem.seal.base.action.ProjBaseAction;
import com.wisemoresystem.seal.base.tip.Tip;
import com.wisemoresystem.seal.model.t_bank;
import com.wisemoresystem.seal.model.t_bank_verify;
import com.wisemoresystem.seal.model.t_user;
import com.wisemoresystem.seal.service.AuthService;
import com.wisemoresystem.seal.service.BankService;
import com.wisemoresystem.seal.service.JobService;
import com.wisemoresystem.seal.service.RoleService;
import com.wisemoresystem.seal.service.UserService;
import com.wisemoresystem.seal.util.json.JsonTools;
import com.wisemoresystem.seal.util.md5.MD5;
import com.wisemoresystem.seal.web.WebTools;

/**
 * 人员管理
 * 
 * @author Administrator
 *
 */
@Controller("ixUserInfo")
@Scope("prototype")
@RequestMapping("/ix/user/info")
public class UserInfoAction extends ProjBaseAction {

	@Autowired
	private JobService jobService;
	@Autowired
	private UserService userService;
	@Autowired
	private BankService bService;
	@Autowired
	private RoleService roleService;
	@Autowired
	private AuthService authService;

	/**
	 * 机构树
	 * 
	 * @param bank_id
	 *            机构id
	 * @return
	 */
	@RequestMapping("/tree")
	public ModelAndView tree(String bank_id) {

		List<Map<String, Object>> list = bService.selectUserBank();

		String bankJson = JsonTools.toJson(list);

		ModelAndView mv = new ModelAndView("/jsp/user/info/tree");
		mv.addObject("bankJson", bankJson);
		mv.addObject("bank_id", bank_id);
		return mv;
	}

	/**
	 * 机构人员列表
	 * 
	 * @param bank_id
	 *            机构id
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(String bank_id) {
		List<String> ulist = authService.selectNotAuth();
		if (ChkTools.isNotNull(ulist)) {
			for (String user_id : ulist) {
				t_user user = oc.findObjById(user_id, t_user.class);
				user.setUser_level("非主管");
				oc.updateObj(user);
			}
		}
		List<Map<String, Object>> userList = userService.selectUserLists(
				bank_id, page);
		t_bank bank = oc.findObjById(bank_id, t_bank.class);
		ModelAndView mv = new ModelAndView("/jsp/user/info/list");
		mv.addObject("userList", userList);
		mv.addObject("bank", bank);
		mv.addObject("bank_id", bank_id);
		return mv;
	}

	/**
	 * 修改人员详细信息
	 * 
	 * @param bank_id
	 *            机构id
	 * @return
	 */
	@RequestMapping("/add_form")
	public ModelAndView add_form(String bank_id) {
		List<Map<String, Object>> jobList = jobService.selectJobList();

		List<Map<String, Object>> role = roleService.selectRoleList(bank_id);
		t_bank bank = oc.findObjById(bank_id, t_bank.class);

		ModelAndView mv = new ModelAndView("/jsp/user/info/add_form");
		mv.addObject("role", role);
		mv.addObject("bank", bank);
		mv.addObject("jobList", jobList);
		return mv;
	}

	/**
	 * 人员添加
	 * 
	 * @param user
	 *            用户
	 * @return
	 */
	@RequestMapping("/add")
	public ModelAndView add(t_user user) {
		user.setPwd(MD5.md5("123456"));
		if (ChkTools.isNull(user.getIs_auth())) {
			user.setIs_auth("否");
		}
		user.setUser_id(WebTools.getCurrentUserId());
		user.setUser_flag("新建人员审核");
		oc.saveObj(user);
		String before = JsonTools.toJson(user);

		t_bank_verify verify = new t_bank_verify();
		verify.setSubmit_user_id(WebTools.getCurrentUserId());
		verify.setBefore_v(before);
		verify.setBefore_bank_id(user.getBank_id());
		verify.setAfter_v("");
		verify.setFlag_note("新建人员审核");
		verify.setFlag_type("人员审核");
		verify.setUser_id(WebTools.getCurrentUserId());
		oc.saveObj(verify);

		logT.logH("新建待审核人员", t_user.class, user.get_id());

		super.setTipMsg("操作成功！", Tip.Type.success);
		ModelAndView mv = new ModelAndView("redirect:tree.htm");
		mv.addObject("bank_id", user.getBank_id());
		return mv;
	}

	/**
	 * 人员修改详细信息
	 * 
	 * @param user_id
	 *            要修改的人员id
	 * @return
	 */
	@RequestMapping("/edit_form")
	public ModelAndView edit_form(String user_id) {
		List<Map<String, Object>> jobList = jobService.selectJobList();

		t_user user = oc.findObjById(user_id, t_user.class);
		t_bank bank = oc.findObjById(user.getBank_id(), t_bank.class);
		List<Map<String, Object>> role = roleService.selectRoleList(user
				.getBank_id());

		ModelAndView mv = new ModelAndView("/jsp/user/info/edit_form");
		mv.addObject("user", user);
		mv.addObject("bank", bank);
		mv.addObject("role", role);
		mv.addObject("jobList", jobList);
		return mv;
	}

	/**
	 * 人员修改
	 * 
	 * @param user
	 *            用户
	 * @param user_id
	 *            用户id
	 * @return
	 */
	@RequestMapping("/edit")
	public ModelAndView edit(t_user user, String user_id) {

		t_user userDb = oc.findObjById(user_id, t_user.class);
		String before = JsonTools.toJson(userDb);

		if (ChkTools.isNull(user.getIs_auth())) {
			userDb.setIs_auth("否");
		}

		userDb.setUser_flag("修改待审核");
		oc.updateObj(userDb);

		user.set_id(user_id);

		String after = JsonTools.toJson(user);

		t_bank_verify verify = new t_bank_verify();
		verify.setSubmit_user_id(WebTools.getCurrentUserId());
		verify.setBefore_v(before);
		verify.setBefore_bank_id(userDb.getBank_id());
		verify.setAfter_bank_id(user.getBank_id());
		verify.setAfter_v(after);
		verify.setFlag_note("修改待审核");
		verify.setFlag_type("人员审核");
		oc.saveObj(verify);

		logT.logH("修改人员待审核", t_user.class, userDb.get_id());

		super.setTipMsg("操作成功！", Tip.Type.success);
		ModelAndView mv = new ModelAndView("redirect:tree.htm");
		mv.addObject("bank_id", user.getBank_id());
		return mv;
	}

	/**
	 * 修改人员状态
	 * 
	 * @param sta
	 *            状态
	 * @param user_id
	 *            用户id
	 * @throws IOException
	 */
	@RequestMapping("/sta")
	public void sta(String sta, String user_id) throws IOException {
		resp.setCharacterEncoding("utf-8");

		t_user user = oc.findObjById(user_id, t_user.class);
		user.setUser_flag(sta);
		oc.updateObj(user);

		String before = JsonTools.toJson(user);

		t_bank_verify verify = new t_bank_verify();
		verify.setSubmit_user_id(WebTools.getCurrentUserId());
		verify.setBefore_v(before);
		verify.setBefore_bank_id(user.getBank_id());
		verify.setAfter_v("");
		verify.setFlag_note(sta);
		verify.setFlag_type("人员审核");
		oc.saveObj(verify);

		logT.logH("修改人员状态待审核", t_user.class, user.get_id());

		resp.getWriter().print("success");
		resp.getWriter().flush();

	}

	@RequestMapping("/check_code")
	public void check_code(String code, String user_id) throws IOException {
		boolean life = true;
		List<Map<String, Object>> user = userService.chkCode(code);
		if (ChkTools.isNotNull(user)) {
			if (!user.get(0).get("_id").equals(user_id)) {
				life = false;
			}
		}

		resp.getWriter().print(life);
		resp.getWriter().flush();
	}

	/**
	 * 密码重置
	 * 
	 * @param user_id
	 *            用户id
	 * @throws IOException
	 */
	@RequestMapping("/repass")
	public void repass(String user_id) throws IOException {

		t_user user = oc.findObjById(user_id, t_user.class);
		user.setPwd(MD5.md5("123456"));
		oc.updateObj(user);

		logT.logH("执行重置密码", t_user.class, user.get_id());

		resp.getWriter().print(true);
		resp.getWriter().flush();
	}

	/**
	 * 检查boying账号
	 * 
	 * @param boeing
	 *            boeing账号
	 * @param user_id
	 *            用户id
	 * @throws IOException
	 */
	@RequestMapping("/check_boeing")
	public void check_boeing(String boeing, String user_id) throws IOException {
		boolean life = true;
		List<Map<String, Object>> user = userService.chkboEing(boeing);
		if (ChkTools.isNotNull(user)) {
			if (!user.get(0).get("_id").equals(user_id)) {

				life = false;
			}
		}
		resp.getWriter().print(life);
		resp.getWriter().flush();
	}

}
